import scrapy
import json
from tqdm import tqdm
from scrapys.wudiSpyder.wudiSpyder.items import OddsSportteryBasketBallMatchItem,SportteryBasketBallMatchItem


class SportteryBasketballSpider(scrapy.Spider):
    name = "SportteryBasketballSpider"
    allowed_domains = ["webapi.sporttery.cn"]
    start_urls = ["https://webapi.sporttery.cn/gateway/uniform/basketball/getMatchListV2.qry?clientCode=3001"    #   篮球-中国竞猜网页面
    ]

    def parse(self, response):
        print("中国竞彩网--篮球》》》》》")
        res = json.loads(response.text)

        # 获取所有的日期比赛
        matchInfoList = res.get("value").get("matchInfoList")
        # 每天的比赛
        for i in tqdm(range(len(matchInfoList))):
            subMatchList = matchInfoList[i].get("subMatchList")
            for j in range(len(subMatchList)):
                m = subMatchList[j]
                matchId = m.get("matchId")
                businessDate = m.get("businessDate")
                homeTeamAbbName = m.get("homeTeamAbbName")
                homeTeamId = m.get("homeTeamId")
                awayTeamAbbName = m.get("awayTeamAbbName")
                awayTeamId = m.get("awayTeamId")
                leagueAbbName = m.get("leagueAbbName")
                matchDate = m.get("matchDate")

                matchNumStr = m.get("matchNumStr")
                matchStatus = m.get("matchStatus")
                matchTime = m.get("matchTime")
                sellStatus = m.get("sellStatus")

                # 提取基本信息
                match_item = SportteryBasketBallMatchItem()
                match_item['matchId'] = matchId
                match_item['businessDate'] = businessDate
                match_item['homeTeamAbbName'] = homeTeamAbbName
                match_item['homeTeamId'] = homeTeamId
                match_item['awayTeamAbbName'] = awayTeamAbbName
                match_item['awayTeamId'] = awayTeamId
                match_item['leagueAbbName'] = leagueAbbName
                match_item['matchDate'] = matchDate
                match_item['matchNumStr'] = matchNumStr
                match_item['matchStatus'] = matchStatus
                match_item['matchTime'] = matchTime
                match_item['sellStatus'] = sellStatus
                yield match_item
                if sellStatus =="1":
                    # 再次去找奖金数据
                    bonus_page = f"https://webapi.sporttery.cn/gateway/uniform/basketball/getFixedBonusV3.qry?clientCode=3001&&matchId={matchId}"
                    yield response.follow(bonus_page, callback=self.parse_other)


    # 处理 胜负彩 停售时间及其他信息
    def parse_other(self, response):

        res = json.loads(response.text)
        v = res.get("value")
        oddsHistory = v.get("oddsHistory")
        matchId = oddsHistory.get("matchId")
        mnlList = oddsHistory.get("mnlList")    # 胜负
        hdcList = oddsHistory.get("hdcList")  # 让分胜负
        hiloList = oddsHistory.get("hiloList")  # 大小分
        wnmList = oddsHistory.get("wnmList")   # 胜分差
        matchNum = v.get("matchInfo").get("matchNum")     # 比赛场次
        singleList = oddsHistory.get("singleList")  # 单关
        # 胜负
        sf_s,sf_f = 0,0
        if mnlList:
            had = mnlList[-1]
            sf_s = float(had.get("h"))
            sf_f = float(had.get("a"))
        sf_bonus = "{},{}".format(sf_s, sf_f)

        # 让分胜负
        rsf_s,rsf_f = 0,0
        goalLine = "0"
        if hdcList:
            for k,v in hdcList.items():
                goalLine = k
                hhad = hdcList[k][-1]
                rsf_s = float(hhad.get("h"))
                rsf_f = float(hhad.get("a"))
        rsf_bonus = "{},{}".format(rsf_s, rsf_f)

        # 大小分
        dx_d,dx_x = 0,0
        dxLine = "0"
        if hiloList:
            for k,v in hiloList.items():
                dxLine = k
                dx = hiloList[k][-1]
                dx_d = float(dx.get("h"))
                dx_x = float(dx.get("l"))
        dx_bonus = "{},{}".format(dx_d, dx_x)

        # 胜分差
        wa, wb, wc,wd, we, wf,la,lb,lc,ld,le,lf = 0,0,0,0,0,0,0,0,0,0,0,0
        if wnmList:
            org = wnmList[-1]
            wa = float(org.get("w1"))
            wb = float(org.get("w2"))
            wc = float(org.get("w3"))
            wd = float(org.get("w4"))
            we = float(org.get("w5"))
            wf = float(org.get("w6"))
            la = float(org.get("l1"))
            lb = float(org.get("l2"))
            lc = float(org.get("l3"))
            ld = float(org.get("l4"))
            le = float(org.get("l5"))
            lf = float(org.get("l6"))

        wnm_bonus = "{},{},{},{},{},{},{},{},{},{},{},{}".format(wa, wb, wc,wd, we, wf,la,lb,lc,ld,le,lf)
        # 是否单关解析
        MNL_single = singleList.get('MNL')[0].get("single")  # 胜负
        HDC_single = singleList.get('HDC')[0].get("single")     # 让分
        HILO_single = singleList.get('HILO')[0].get("single")   #大小
        WNM_single = singleList.get('WNM')[0].get("single")     # 胜分差

        single = "{},{},{},{}".format(MNL_single, HDC_single, HILO_single,WNM_single)
        # 保存奖金指数
        odd_item = OddsSportteryBasketBallMatchItem()
        odd_item["matchId"] = matchId
        odd_item["sf_bonus"] = sf_bonus
        odd_item["rsf_bonus"] = rsf_bonus
        odd_item["dx_bonus"] = dx_bonus
        odd_item["wnm_bonus"] = wnm_bonus
        odd_item["single"] = single
        odd_item["goalLine"] = goalLine
        odd_item["dxLine"] = dxLine
        odd_item["matchNum"] = matchNum

        yield odd_item
